﻿
#include "pch.h"
#include "spk_ulog.h"
#include <stdint.h>
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <stdarg.h>

#ifndef __AFX_H__
#define MFC_SUPPORT 0
#else 
#define MFC_SUPPORT 1
#endif 

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define CSI_START                      "\033["
#define CSI_END                        "\033[0m"

#define F_BLACK                        "30m"
#define F_RED                          "31m"
#define F_GREEN                        "32m"
#define F_YELLOW                       "33m"
#define F_BLUE                         "34m"
#define F_MAGENTA                      "35m"
#define F_CYAN                         "36m"
#define F_WHITE                        "37m"

void _set_console_color(int level)
{
#ifdef _WIN32
	/*10 绿色 12 红色 14 黄色 15 白色*/
	unsigned short color_index = 0;
	switch (level) {
		case LOG_LVL_INFO:     color_index = (FOREGROUND_GREEN); break;
		case LOG_LVL_WARNING:  color_index = (FOREGROUND_RED | FOREGROUND_GREEN); break;
		case LOG_LVL_ERROR:    color_index = (FOREGROUND_RED); break;
		case LOG_LVL_DBG:      color_index = 15; break;
		default: break;
	}
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_index);
#else 

	switch (level) {
		case LOG_LVL_INFO:     printf(F_GREEN); break;
		case LOG_LVL_WARNING:  printf(F_YELLOW); break;
		case LOG_LVL_ERROR:    printf(F_RED); break;
		case LOG_LVL_DBG:      printf(F_WHITE); break;
		default: break;
	}


#endif 
}

/*后台日志输出*/
static void do_output(uint32_t level, const char* tag, int is_raw, const char* log_buf, size_t log_len)
{
#if	MFC_SUPPORT

	static CString str;
	str  = tag;
	str += ":";
	str += log_buf;

	/*如果支持MFC，将在出错的情况下弹出对话框*/
	if (level <= LOG_LVL_ERROR) {	
		AfxMessageBox(str, MB_ICONERROR);
	}
	//int pos = 1;
	/*更新状态栏*/
	//if(AfxGetMainWnd()) ::SendMessage(AfxGetMainWnd()->m_hWnd, MAIN_DLG_STBAR, (WPARAM)&str, (LPARAM)(pos));
#endif 

#ifdef _WIN32
	_set_console_color(level);
	printf("%s:%s\n",tag,log_buf);
	_set_console_color(LOG_LVL_DBG);
#else 
	printf(CSI_START);
	_set_console_color(level);
	if(strlen(tag))
	{
		printf("%s:%s\n",tag,log_buf);
	}
	else
	{
		printf("%s\n",log_buf);
	}
	printf(CSI_END);
#endif 
}

/*多核环境下需要设计访问锁*/
static void output_lock(void)
{
}

static void output_unlock(void)
{
}

size_t ulog_formater(char* log_buf, uint32_t level, const char* tag, int newline, const char* format, va_list args)
{
	int len = vsprintf(log_buf, format, args);
	return len;
}


/**
 * output the log by variable argument list
 *
 * @param level level
 * @param tag tag
 * @param newline has_newline
 * @param format output format
 * @param args variable argument list
 */
void ulog_voutput(uint32_t level, const char *tag, int newline, const char *format, va_list args)
{
    char log_buf[ULOG_LINE_BUF_SIZE];

    RT_ASSERT(level <= LOG_LVL_DBG);
    RT_ASSERT(tag);
    RT_ASSERT(format);

    /* lock output */
    output_lock();
    size_t log_len = ulog_formater(log_buf, level, tag, newline, format, args);

    /* do log output */
    do_output(level, tag, 0, log_buf, log_len);

    /* unlock output */
    output_unlock();
}

void ulog_output(uint32_t level, const char *tag,const char *format, ...)
{
    va_list args;
    /* args point to the first variable parameter */
    va_start(args, format);
    ulog_voutput(level, tag,0, format, args);
    va_end(args);
}




